package com.liuqh.tag.config.shiro;

import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.request.ServletRequestAttributes;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author huangxiong
 * @Date 2019/7/31
 **/
@Component
@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor {
	@Override
	public void apply(RequestTemplate template) {
		Map<String, String> headers = getHeaders(getHttpServletRequest());
		String cookie = headers.get("cookie");
		log.info("cookie:{}", headers.get("cookie"));
		String token = headers.get("authorization");
		if (StringUtils.isNotBlank(cookie)) {
			template.header("cookie", headers.get("cookie"));
		} else if (StringUtils.isNotBlank(token)) {
			template.header("authorization", token);
		}

	}

	private HttpServletRequest getHttpServletRequest() {
		try {
			return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		} catch (Exception e) {
			log.error("HttpServletRequest :{}", e);
			return null;
		}
	}

	private Map<String, String> getHeaders(HttpServletRequest request) {
		if (null == request) {
			return null;
		}
		Map<String, String> map = new LinkedHashMap<>();
		Enumeration<String> enumeration = request.getHeaderNames();
		while (enumeration.hasMoreElements()) {
			String key = enumeration.nextElement().toLowerCase();
			String value = request.getHeader(key);
			map.put(key, value);
		}
		return map;
	}

	@Bean
	public RequestContextListener requestContextListener() {
		return new RequestContextListener();
	}

}
